package statsyslogic

import (
	"context"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/stat"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/key/walletkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"
	"strconv"
	"time"

	"navi-mumbai/app/rpc/stat/internal/svc"
	"navi-mumbai/app/rpc/stat/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserGameDailyOrderStatLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserGameDailyOrderStatLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserGameDailyOrderStatLogic {
	return &UserGameDailyOrderStatLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 每日用户游戏订单数据统计
func (l *UserGameDailyOrderStatLogic) UserGameDailyOrderStat(in *pb.UserGameDailyOrderStatReq) (*pb.UserGameDailyOrderStatResp, error) {
	defer utils.RecoverPanic()

	var start, end *time.Time
	if in.StartTime != "" {
		loc, _ := time.LoadLocation(dbkey.DateTimeZone)
		t, err := time.ParseInLocation(dbkey.DateTimeCNFormat, in.StartTime, loc)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, err.Error())
		}
		start = &t
	}
	if in.EndTime != "" {
		loc, _ := time.LoadLocation(dbkey.DateTimeZone)
		t, err := time.ParseInLocation(dbkey.DateTimeCNFormat, in.EndTime, loc)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, err.Error())
		}
		end = &t
	}

	var ds stat.UserGameDailyOrderStat
	ds.StatDay, _ = strconv.ParseInt(start.Format(dbkey.DateFormat2), 10, 64)
	ds.ProviderId = in.ProviderId
	ds.GameId = in.GameId
	var err error

	// 订单金额
	ds.OrderAmount, err = l.svcCtx.UserGameOrderFlowModel.SumOrderAmount(l.ctx, in.ProviderId, in.GameId, start, end)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
	}
	l.Logger.Infof("UserGameDailyOrderStat. OrderAmount: %+v, GameId: %+v", ds.OrderAmount, in.GameId)

	// 成功下单数
	ds.OrderCount, err = l.svcCtx.UserGameOrderFlowModel.CountOrderCount(l.ctx, 0, in.ProviderId, in.GameId, 0, walletkey.GameOrderStateSuccess, start, end)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
	}

	// 成功下单人数
	ds.OrderUserCount, err = l.svcCtx.UserGameOrderFlowModel.CountUser(l.ctx, in.ProviderId, in.GameId, start, end)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
	}

	// 返利金额
	ds.ReturnAmount, err = l.svcCtx.UserGameOrderFlowModel.SumReturnAmount(l.ctx, in.ProviderId, in.GameId, start, end)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
	}

	// 返利订单数
	ds.ReturnCount, err = l.svcCtx.UserGameOrderFlowModel.CountReturnOrderCount(l.ctx, 0, in.ProviderId, in.GameId, 0, start, end)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
	}
	// 返利人数
	ds.ReturnUserCount, err = l.svcCtx.UserGameOrderFlowModel.CountReturnUser(l.ctx, in.ProviderId, in.GameId, start, end)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
	}

	_, err = l.svcCtx.UserGameDailyOrderStatModel.Insert(l.ctx, &ds)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, err
	}

	return &pb.UserGameDailyOrderStatResp{}, nil
}
